/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Activity;
import org.ipti.br.tag.entity.Activity_;
import org.ipti.br.tag.entity.UnitySchedule;
import org.ipti.br.tag.entity.UnitySchedule_;
import org.ipti.br.tag.entity.WorkTopic;
import org.ipti.br.tag.entity.WorkTopic_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author luancorumba
 */
@Stateless
public class ActivityFacade extends AbstractFacade<Activity> {
    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ActivityFacade() {
        super(Activity.class);
    }
    
    public List<Activity> list(Calendar day, UnitySchedule unitySchedule) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Activity> cq = cb.createQuery(Activity.class);
        Root<Activity> rt = cq.from(Activity.class);
        
        Predicate p = cb.equal(rt.get(Activity_.efetivation), day);
        Predicate p1 = cb.equal(rt.get(Activity_.unitySchedule).get(UnitySchedule_.id), unitySchedule.getId());
        
        cq.where(p,p1);
        
        return getEntityManager().createQuery(cq).getResultList();
    }
    
    public List<Activity> list(WorkTopic topic) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Activity> cq = cb.createQuery(Activity.class);
        Root<Activity> rt = cq.from(Activity.class);
        
        cq.where(cb.equal(rt.get(Activity_.workTopic).get(WorkTopic_.id), topic.getId()));
        
        return getEntityManager().createQuery(cq).getResultList();
    }
}
